<!--
 * @Author: thinker_cy
 * @Date: 2025-07-02 16:44:03
 * @FilePath: \weimai-admin\src\pages\login\index.vue
 * @Description: 
-->
<script setup>
import { reactive, ref, onMounted } from 'vue'
import { User, Lock, Refresh } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router';
import http from '@/api/index.js'

const router = useRouter();

const ruleFormRef = ref()

const userNameValid = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入用户名'))
  } else {
    callback()
  }
}
const validatePassValid = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    if (formData.user_name !== '') {
      if (!ruleFormRef.value) return
      ruleFormRef.value.validateField('user_name')
    }
    callback()
  }
}

const formData = reactive({
  user_name: '',
  password: '',
  captcha: ''
})

const captchaUrl = ref('')

const rules = reactive({
  user_name: [{ validator: userNameValid, trigger: 'blur' }],
  password: [{ validator: validatePassValid, trigger: 'blur' }],
  captcha: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
})

const getCaptchaCode = () => {
  // 添加时间戳防止缓存
  captchaUrl.value = 'https://adminwm.douyar.cc/admin/captcha/captcha?t=' + Date.now()
}

onMounted(() => {
  getCaptchaCode()
})

const submitForm = async (formEl) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      const loginParams = {
        ...formData
      }
      const result = await http.login(loginParams)
      localStorage.setItem('userName', result.user_name)
      // localStorage.setItem('userInfo', JSON.stringify(result.data))
      router.push('/')
    } else {
      console.log('error submit!')
    }
  })
}

const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<template>
  <div style="display: flex; height: 100%;">
    <div style="width: 60%; display: flex;flex-direction: column;align-items: center;justify-content: center;">
      <!-- <img src="@/assets/left-bg.png" alt="" style="width: 100%;"> -->
      <h2 class="text-2xl">威脉医疗</h2>
    </div>
    <div style=" width: 50%;display: flex;flex-direction: column;align-items: center;justify-content: center;">
      <div style="margin-bottom: 50px;font-size: 28px;">欢迎登录</div>
      <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="formData"
        status-icon
        :rules="rules"
        label-width="auto"
        @keydown.enter="submitForm(ruleFormRef)"
      >
        <el-form-item label="" prop="user_name">
          <el-input
            v-model="formData.user_name"
            :prefix-icon="User"
            placeholder="请输入用户名"
            clearable
          />
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input v-model="formData.password" type="password" :prefix-icon="Lock" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="" prop="captcha">
          <div style="display: flex; gap: 10px;">
            <el-input
              v-model="formData.captcha"
              placeholder="请输入验证码"
              clearable
              style="flex: 1;"
            />
            <div style="display: flex; align-items: center;">
              <img
                :src="captchaUrl"
                alt="验证码"
                style="height: 40px; cursor: pointer;"
                @click="getCaptchaCode"
              />
              <el-icon @click="getCaptchaCode" style="margin-left: 5px; cursor: pointer;">
                <Refresh />
              </el-icon>
            </div>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)" style="width: 100%;">
            登录
          </el-button>
          <!-- <el-button @click="resetForm(ruleFormRef)">注册</el-button> -->
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>